/**
 * Created with JetBrains WebStorm.
 * User: thaidoanhong
 * Date: 3/25/13
 * Time: 8:54 PM
 * To change this template use File | Settings | File Templates.
 */
var MyElement = Class.create({
    initialize: function(o){
        //o --> {parent}
        this.attributeList = new MyAttributeList();
        this.operationList = new MyOperationList();

        var _o = o || {};

        this.parent = _o.parent || null;
        this.childs = new Array();

        this.coreElements = new Array();

        this.id = "";

        this.divTag = new Element('div');
        $(this.divTag).setStyle({position: "absolute"});

        if(this.parent !== null){
            $(this.parent.divTag).insert(this.divTag);
        }
        else{
            $(document.body).insert(this.divTag);
        }
        console.log('new MyElement');



    },
    initAttributeList: function(o){
        //o la jsonobject
        var _this = this;

        $j.each(o.specs, function(){
            //Neu la id thi luu truc tiep len tren element
            if(this.type == "id"){
                _this.id = this.value;
                $(_this.divTag).writeAttribute(this.key, this.value);


            }
            //neu la css thi gan Style truc tiep cho divTag va luu vao AttributeList
            else if(this.type == "css"){
                _this.attributeList.add(new MyAttribute(this));

                var sty = new Object();
                sty[this.key] = this.value;
                $(_this.divTag).setStyle(sty);
            }
            //neu la class thi tien hanh khoi tao class voi tham so dau vao la value
            else if(this.type == "core"){
                var newInst = new window[this.cls](this.value, _this.divTag);
                _this.coreElements.push(newInst);
            }
            else if(this.type == "elementAttribute"){
                _this.attributeList.add(new MyAttribute(this));
                $(_this.divTag).writeAttribute(this.key, this.value);
            }
        });

        //bind event click
        $j("#"+ _this.id).bind("click",function(){
            var editor = new PropertyEditor(_this,"#editor",_this.id);
            editor.init();
            editor.reload(_this.attributeList);
//            editor.onChange = function(sender,params){
//                   _this.setValueByName(params.key,params.value);
//                    editor.reload(_this.attributeList);
            //    console.log("co vao day");
   //         }
        })

        console.log("init attributeList done");
    },
    initOperationList: function(o){
        var _this = this;

        $j.each(o.operations, function(){
            var newOp = new MyOperation(this);
            _this.operationList.add(newOp);
        });
        console.log("init operationList done");

    },
    getValueFromKey: function(key){
        return this.attributeList.getValueFromKey(this, key);
    },
    getValueFromName: function(name){
        return this.attributeList.getValueFromName(this, name);
    },
    setValueByKey: function(key, value){
        return this.attributeList.setValueByKey(this, key, value);
    },
    setValueByName: function(name, value){
        return this.attributeList.setValueByName(this, name, value);
    },
    update: function(){
        this.coreElements.each(function(item){
            item.update();
        });

        this.childs.each(function(item){
            item.update();
        });
    },
    draw: function(){

    },
    addChild: function(c){
        //c la mot Element
        this.childs.push(c);
        if(c.parent === null){
            $(c.divTag).remove();
        }
        this.divTag.insert(c.divTag);

        c.parent = this;
    },
    removeChild: function(id){
        var c;
        var found = false;

        for(var i = 0 ; i < this.childs.length ; i++){
            c = this.childs[i];

            if(c.id == id){
                c.parent = null;

                $(c.divTag).remove();
                this.childs[i] = null;
                found = true;
                break;
            }
        }

        if(found){
            this.childs = this.childs.compact();
            return c;
        }
        return null;
    },
    getElementById: function(id){
        if(this.id == id){
            return this;
        }

        var returnVal = null;

        for (var i = 0; i < this.childs.length; i++) {
            returnVal = this.childs[i].getElementById(id);
            if(returnVal !== null){
                break;
            }
        }

        return returnVal;
    },
    execute: function(name, target, args){
        this.operationList.execute(this, name, target, args);
    }
});

